package com.jxd.controller;

import com.jxd.config.ApplicationContextRegister;
import com.jxd.entity.MesHead;
import com.jxd.hbase.analysis.FaultStatistics;
import com.jxd.hbase.analysis.TwoMachineSwitchover;
import com.jxd.model.EventRecord;
import com.jxd.repositories.MesHeadRepositories;
import com.jxd.service.EventRecordService;
import com.jxd.service.ShipInformationService;
import com.jxd.threaduntil.CommMessSwitchCoverThread;
import com.jxd.threaduntil.FaultStatisticsthread;
import com.jxd.threaduntil.MaintainSwitchCoverss;
import com.jxd.threaduntil.TwoMachineSwitchoverThread;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.util.*;

@Controller
@RequestMapping("/hbaseindex")
public class HbaseIndexController {
    @Autowired
    @Qualifier("transportClient")
    private TransportClient client;
    @Autowired
    private ElasticsearchTemplate Template;
    @Autowired
    MesHeadRepositories mesHeadRepositories;
    @Autowired
    private  EventRecordService eventRecordService;
    @Autowired
    private ShipInformationService shipInformationService;
    @PostMapping("/addmesheadindex")
    @ResponseBody
    public boolean addMesheadIndex(@RequestBody MesHead mesHead){
        boolean flag = false;
        try {
            System.out.println(mesHead.toString());
            mesHeadRepositories.save(mesHead);
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    @PostMapping("/addmesheadindexs")
    @ResponseBody
    public boolean addMesheadIndexs(@RequestBody List<MesHead> mesHeads){
        boolean flag = false;
        try {
            System.out.println(mesHeads.size());
            mesHeadRepositories.saveAll(mesHeads);
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    @PostMapping("/deletemesheadindexs")
    @ResponseBody
    public boolean deleteMesheadIndexs(){
        boolean flag = false;
        try {
            mesHeadRepositories.deleteAll();
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    @PostMapping("/getmesheadindexbyid")
    @ResponseBody
    public MesHead getMesheadIndexByid( @RequestBody HashMap<String, String> map ){
        String id = map.get("id");
        MesHead mesHead = new MesHead();
            try {
                mesHead = mesHeadRepositories.findAllById(id);
            }catch (Exception e){
                e.printStackTrace();
            }
            return mesHead;
    }
    @PostMapping("/getallmesheadindex")
    @ResponseBody
    public List<MesHead> getAllMesheadIndex( ){
        List<MesHead> mesHeads = new ArrayList<>();
        try {
            mesHeads = mesHeadRepositories
                    .findAllByFileNameAndShipId(
                            "20200714.data",
                            "123",
                            Sort.by(Sort.Order.asc("mesSignId")));
        }catch (Exception e){
            e.printStackTrace();
        }
        return mesHeads;
    }
@PostMapping("getindexbymesid")
@ResponseBody
    public List<MesHead> getindexBymesId(@RequestBody HashMap<String, String> map ){
    String shipid = map.get("shipId");
    String filename = map.get("fileName");
    String mesid = map.get("mesId");
    List<MesHead> mesHeads = new ArrayList<>();
    try {
        mesHeads= mesHeadRepositories.findAllByFileNameAndMesIdAndShipId(filename,mesid,shipid);

    }catch (Exception e){
        e.printStackTrace();
    }
return mesHeads;
}
    @PostMapping("getindexbymessignbefore")
    @ResponseBody
    public List<MesHead> getIndexByMesSignBefore(@RequestBody HashMap<String, String> map ){
        String shipid = map.get("shipId");
        String filename = map.get("fileName");
        Long mesSignId = Long.valueOf(map.get("mesSignId"));
        List<MesHead> mesHeads = new ArrayList<>();
        try {
            mesHeads= mesHeadRepositories.findAllByMesSignIdBeforeAndFileNameAndShipId(mesSignId,filename,shipid);

        }catch (Exception e){
            e.printStackTrace();
        }
        return mesHeads;
    }
    @PostMapping("getindexbymessignbetween")
    @ResponseBody
    public List<MesHead> getindexByMesSignBetween(@RequestBody HashMap<String, String> map ){
        Long endMesSignId = Long.valueOf(map.get("endMesSignId"));
        Long stMesSignId = Long.valueOf(map.get("stMesSignId"));
        List<MesHead> mesHeads = new ArrayList<>();
        try {
            mesHeads= mesHeadRepositories.findAllByMesSignIdBetween(stMesSignId,endMesSignId);

        }catch (Exception e){
            e.printStackTrace();
        }
        return mesHeads;
    }
    @GetMapping(value = "/getselectByPrimaryKey")
    @ResponseBody
    public EventRecord selectByPrimaryKey(String id){
        EventRecord eventRecord =eventRecordService.selectByPrimaryKey(id);
        System.out.println(eventRecord.toString());
        return eventRecord;
    }

    @GetMapping(value = "/selectbyXtbh")
    @ResponseBody
    public String  selectbyXtbh(String id){
        System.out.println(id);
        return shipInformationService.selectbyXtbh(id);
    }
    @GetMapping("/fau")
    public  void getfau(){
        long st = new Date().getTime();
        System.out.println("开始");
        String filename = "20200803.data";
        String shipid = "123";
        String mesId = "61456";
        String xtid =shipInformationService.selectbyXtbh(shipid);
        String  workMesId ="41008";

        long end = new Date().getTime();
        System.out.println(end-st);
    }
}
